package StudyCode.summary.mycollection;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class MergeListsAndRemoveDuplicates {
	public static List<Integer> mergeListsAndRemoveDuplicates(
			List<Integer> listOne, List<Integer> listTwo) {

		if (listOne.isEmpty()) {
			return listTwo;
		}
		if (listTwo.isEmpty()) {
			return listOne;
		}

		Iterator<Integer> listTwoIterator = listTwo.iterator();
		int nextTwo = listTwoIterator.next();
		List<Integer> result = new ArrayList<Integer>();
		boolean twoNotEmpty = true;

		for (int nextOne : listOne) {

			while (nextOne > nextTwo && twoNotEmpty) {
				result.add(nextTwo);
				if (listTwoIterator.hasNext()) {
					nextTwo = listTwoIterator.next();
				} else {
					twoNotEmpty = false;
					nextTwo = 0;
				}
			}

			if (nextOne == nextTwo) {
				if (listTwoIterator.hasNext()) {
					nextTwo = listTwoIterator.next();
				} else {
					twoNotEmpty = false;
					nextTwo = 0;
				}
			}

			result.add(nextOne);
		}

		if (twoNotEmpty) {
			result.add(nextTwo);
		}
		while (listTwoIterator.hasNext()) {
			result.add(listTwoIterator.next());
		}

		return result;
	}
}
